package com.flyox.game.fivetiger.view;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;

import com.flyox.game.fivetiger.bean.CONSTS;
import com.flyox.game.fivetiger.bean.MsgPackage;
import com.flyox.game.fivetiger.bean.Position;
import com.flyox.game.fivetiger.control.FivetigerControl;
import com.flyox.game.fivetiger.tools.UtilBean;

/**
 * 单个棋子控件
 * 
 * @author sunwei
 * 
 */
public class ChessManLabel extends JLabel implements MouseListener {
	private static final long serialVersionUID = -7540485333786329025L;
	private int chessType;
	private Position pos;
	FivetigerControl control;
	UtilBean utilBean;

	public ChessManLabel(Position pos, FivetigerControl control) {
		super();
		this.pos = pos;
		this.control = control;
		this.chessType = control.getDoc().getLayout()[pos.row][pos.col];
		switch (chessType) {
		case CONSTS.CHESS_EMPTY:
			break;
		case CONSTS.CHESS_ZERO:
			setIcon(new ImageIcon(control.getView().zeroImage));
			break;
		case CONSTS.CHESS_THREE:
			setIcon(new ImageIcon(control.getView().threeImage));
			break;
		case CONSTS.CHESS_ZERO_THREE:
			setIcon(new ImageIcon(control.getView().zerotThreeImage));
			break;
		case CONSTS.CHESS_THREE_ZERO:
			setIcon(new ImageIcon(control.getView().threeZeroImage));
			break;
		default:
			break;
		}
		this.setSize(40, 40);
		addMouseListener(this);
		utilBean = new UtilBean();
	}

	/**
	 * 更新成被覆盖的图片
	 */
	// public void setOnCover() {
	// if (chessType == CONSTS.CHESS_ZERO) {
	// setIcon(new ImageIcon(control.getView().threeZeroImage));
	// } else if (chessType == CONSTS.CHESS_THREE) {
	// setIcon(new ImageIcon(control.getView().zerotThreeImage));
	// }
	// }
	//
	// public void undoOnCover() {
	// if (chessType == CONSTS.CHESS_ZERO) {
	// setIcon(new ImageIcon(control.getView().zeroImage));
	// } else if (chessType == CONSTS.CHESS_THREE) {
	// setIcon(new ImageIcon(control.getView().threeImage));
	// }
	// }
	//
	// public void setPos(Position pos) {
	// this.pos = pos;
	// }
	/**
	 * 处理压棋动作
	 * 
	 * @param pos
	 */
	private void processCoverClick(Position pos) {
		// 检查是否选中对方棋子
		if (chessType != control.getDoc().getOtherSideTeamFlag()) {
			return;
		}
		// 已成型不可以压
		if (utilBean.checkShapeRemove(control.getDoc().getLayout(), pos) > 0) {
			return;
		}
		control.getDoc().setCanRemoveChessCount(control.getDoc().getCanRemoveChessCount() - 1);// doc
		// count
		// 无棋可压
		if (control.getDoc().getCanRemoveChessCount() > 0 && !utilBean.haveAnyChessRemove(control.getDoc().getLayout(), control.getDoc().getOtherSideTeamFlag())) {
			control.getDoc().setCanRemoveChessCount(0);
		}
		control.getDoc().setOnCover(pos);//

		// 发送信息给对方
		MsgPackage msgPackage = new MsgPackage();
		msgPackage.setCmd(CONSTS.CMD_COVER);
		msgPackage.setCmdPlayer(control.getDoc().getCurrentPlayer());
		msgPackage.setFirstPos(pos);
		if (control.getDoc().getCanRemoveChessCount() > 0) {
			msgPackage.setCurrentState(CONSTS.CMD_COVER);
			msgPackage.setCurrentPlayer(control.getDoc().getName());
			msgPackage.setCanRemoveChessCount(control.getDoc().getCanRemoveChessCount());
		} else if (control.getDoc().isFull()) {// remove all cover
			msgPackage.setCurrentState(CONSTS.CMD_MOVE);
			msgPackage.setCurrentPlayer(control.getDoc().getOtherSideName());
			msgPackage.cloneFullLayout(control.getDoc().getLayout());
			control.getDoc().removeAllCovered();
			msgPackage.showLayout();
		} else {
			msgPackage.setCurrentState(CONSTS.CMD_ADD);
			msgPackage.setCurrentPlayer(control.getDoc().getOtherSideName());
		}
		control.sendMsg(msgPackage);

		// 修改本地doc
		control.getDoc().pushStept(msgPackage);
		control.getDoc().setCurrentPlayer(msgPackage.getCurrentPlayer());
		control.getDoc().setCurrentState(msgPackage.getCurrentState());
	}

	/**
	 * 处理走棋动作
	 * 
	 * @param pos
	 */
	private void processMoveClick(Position pos) {
		// 不是自己的不可以选
		if (chessType != control.getDoc().getTeamFlag()) {
			return;
		}
		// 棋子不能移动不可以选
		if (!utilBean.chessCanMove(control.getDoc().getLayout(), pos)) {
			return;
		}

		Position tempSelectedChessMan = control.getView().layoutJPanel.selectedChessMan;
		if (tempSelectedChessMan == null) {
			control.getView().layoutJPanel.selectedChessMan = pos;
		} else {
			control.getView().layoutJPanel.cml[tempSelectedChessMan.row][tempSelectedChessMan.col].setBorder(BorderFactory.createCompoundBorder(null, null));
			control.getView().layoutJPanel.selectedChessMan = null;
		}
	}

	/**
	 * 处理拔棋动作 输赢判断放在对方
	 * 
	 * @param pos
	 */
	private void processCutClick(Position pos) {
		// 不可以选自己的
		if (chessType == control.getDoc().getTeamFlag()) {
			return;
		}
		// 成型的不可以 拔掉
		if (utilBean.checkShapeRemove(control.getDoc().getLayout(), pos) > 0) {
			return;
		}
		control.getDoc().setCanRemoveChessCount(control.getDoc().getCanRemoveChessCount() - 1);// doc
		// 无棋可拔
		if (control.getDoc().getCanRemoveChessCount() > 0 && !utilBean.haveAnyChessRemove(control.getDoc().getLayout(), control.getDoc().getOtherSideTeamFlag())) {
			control.getDoc().setCanRemoveChessCount(0);
		}
		control.getDoc().cutChess(pos);

		// 发送信息给对方
		MsgPackage msgPackage = new MsgPackage();
		msgPackage.setCmd(CONSTS.CMD_CUT);
		msgPackage.setCmdPlayer(control.getDoc().getCurrentPlayer());
		msgPackage.setFirstPos(pos);
		if (control.getDoc().isOver(control.getDoc().getOtherSideTeamFlag())) {
			control.getDoc().setStateMsg("你赢了");

			msgPackage.setCmd(CONSTS.CMD_FAIL);
			msgPackage.setCurrentPlayer(control.getDoc().getOtherSideName());
			msgPackage.setCurrentState(CONSTS.CMD_ADD);
			msgPackage.setCmdPlayer(control.getDoc().getCurrentPlayer());
			
			JOptionPane.showMessageDialog(control.getView(), "你赢了！", "恭喜", JOptionPane.INFORMATION_MESSAGE, null);
			control.processBegin(control.getDoc().getOtherSideName());
		}else if (control.getDoc().getCanRemoveChessCount() > 0) {
			msgPackage.setCurrentState(CONSTS.CMD_CUT);
			msgPackage.setCurrentPlayer(control.getDoc().getName());
			msgPackage.setCanRemoveChessCount(control.getDoc().getCanRemoveChessCount());
		} else if (control.getDoc().isOtherSideNeedCut()) {// 告诉对方也拔掉一个棋子
			control.getDoc().setOtherSideNeedCut(false);
			msgPackage.setCurrentState(CONSTS.CMD_CUT);
			msgPackage.setCurrentPlayer(control.getDoc().getOtherSideName());
		} else {
			msgPackage.setCurrentState(CONSTS.CMD_MOVE);
			msgPackage.setCurrentPlayer(control.getDoc().getOtherSideName());
		}
		
		control.sendMsg(msgPackage);

		// 修改本地doc
		control.getDoc().pushStept(msgPackage);
		control.getDoc().setCurrentPlayer(msgPackage.getCurrentPlayer());
		control.getDoc().setCurrentState(msgPackage.getCurrentState());
	}

	/**
	 * 颜色变化重新设定
	 */
	public void mouseClicked(MouseEvent e) {
		// 不是当前操作用户点击无效
		if (!control.getDoc().isCurrentPlayerPlay()) {
			return;
		}

		switch (control.getDoc().getCurrentState()) {
		case CONSTS.CMD_COVER:
			processCoverClick(pos);
			break;
		case CONSTS.CMD_MOVE:
			processMoveClick(pos);
			break;
		case CONSTS.CMD_CUT:
			processCutClick(pos);
			break;
		}
	}

	public void mouseEntered(MouseEvent e) {
		setBorder(new SoftBevelBorder(BevelBorder.RAISED));
	}

	public void mouseExited(MouseEvent e) {
		if (control.getView().layoutJPanel.selectedChessMan != null && control.getView().layoutJPanel.selectedChessMan.equals(pos)) {
			setBorder(BorderFactory.createCompoundBorder(new LineBorder(Color.GREEN, 2, true), null));
		} else {
			setBorder(BorderFactory.createCompoundBorder(null, null));
		}
	}

	public void mousePressed(MouseEvent e) {
		setBorder(new SoftBevelBorder(BevelBorder.LOWERED));
	}

	public void mouseReleased(MouseEvent e) {
		if (e.getSource().equals(this)) {
			setBorder(new SoftBevelBorder(BevelBorder.RAISED));
		} else {
			if (control.getView().layoutJPanel.selectedChessMan != null && control.getView().layoutJPanel.selectedChessMan.equals(pos)) {
				setBorder(BorderFactory.createCompoundBorder(new LineBorder(Color.GREEN, 2, true), null));
			} else {
				setBorder(BorderFactory.createCompoundBorder(null, null));
			}
		}
	}

	public boolean equals(Object o) {
		if (o instanceof ChessManLabel) {
			ChessManLabel cml = (ChessManLabel) o;
			if (cml.pos.equals(pos)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

}
